package com.yh.ldzl.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.yh.ldzl.service.QualityCheckWebSocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * WebSocket控制器
 * 提供WebSocket状态检查和测试接口
 */
@RestController
@RequestMapping("/websocket")
public class WebSocketController extends BaseController {

    @Autowired
    private QualityCheckWebSocketService webSocketService;

    /**
     * 获取WebSocket连接状态
     */
    @GetMapping("/status")
    public AjaxResult getWebSocketStatus() {
        Map<String, Object> data = new HashMap<>();
        data.put("connectionCount", webSocketService.getConnectionCount());
        data.put("status", "active");
        return AjaxResult.success("WebSocket服务正常运行", data);
    }

    /**
     * 测试广播消息
     */
    @PostMapping("/broadcast")
    public AjaxResult broadcastMessage(@RequestParam String message, @RequestParam(required = false) String type) {
        if (type == null || type.isEmpty()) {
            type = "test_broadcast";
        }

        Map<String, Object> data = new HashMap<>();
        data.put("timestamp", System.currentTimeMillis());
        data.put("test", true);

        try {
            // 构造简单的测试消息进行广播
            webSocketService.pushToUser("all", type, message, data);
            return AjaxResult.success("广播消息发送成功");
        } catch (Exception e) {
            return AjaxResult.error("广播消息发送失败: " + e.getMessage());
        }
    }

    /**
     * 测试质检单创建通知
     */
    @PostMapping("/test/quality-check-created")
    public AjaxResult testQualityCheckCreated(
            @RequestParam Long inspectionId,
            @RequestParam String inspectionCode,
            @RequestParam(defaultValue = "2") String inspectionType,
            @RequestParam(required = false) String message
    ) {
        try {
            if (message == null || message.isEmpty()) {
                message = "测试质检单创建通知";
            }
            
            webSocketService.pushQualityCheckCreated(inspectionId, inspectionCode, inspectionType, message);
            return AjaxResult.success("测试质检单创建通知发送成功");
        } catch (Exception e) {
            return AjaxResult.error("测试质检单创建通知发送失败: " + e.getMessage());
        }
    }

    /**
     * 测试质检进度更新通知
     */
    @PostMapping("/test/quality-check-progress")
    public AjaxResult testQualityCheckProgress(
            @RequestParam Long inspectionId,
            @RequestParam String progress,
            @RequestParam(required = false) String message
    ) {
        try {
            if (message == null || message.isEmpty()) {
                message = "质检进度更新: " + progress + "%";
            }
            
            webSocketService.pushQualityCheckProgress(inspectionId, progress, message);
            return AjaxResult.success("测试质检进度更新通知发送成功");
        } catch (Exception e) {
            return AjaxResult.error("测试质检进度更新通知发送失败: " + e.getMessage());
        }
    }

    /**
     * 测试质检完成通知
     */
    @PostMapping("/test/quality-check-complete")
    public AjaxResult testQualityCheckComplete(
            @RequestParam Long inspectionId,
            @RequestParam(defaultValue = "1") String result,
            @RequestParam(required = false) String message
    ) {
        try {
            if (message == null || message.isEmpty()) {
                message = "质检完成，结果: " + ("1".equals(result) ? "合格" : "不合格");
            }
            
            webSocketService.pushQualityCheckComplete(inspectionId, result, message);
            return AjaxResult.success("测试质检完成通知发送成功");
        } catch (Exception e) {
            return AjaxResult.error("测试质检完成通知发送失败: " + e.getMessage());
        }
    }
}
